<script setup lang="ts">

import {Lock, User} from "@element-plus/icons-vue";
import {onMounted, reactive, ref, watch} from "vue";
import {LOGIN_API} from "@/api/login";
import {ElMessage} from "element-plus";
import { useRouter } from "vue-router";
const form = reactive({
  username: '',
  password: ''
})
const router = useRouter()
const checkBox = ref(false)
const loading = ref(false)
const login = () => {
  try {
    loading.value = true
    LOGIN_API(form).then((res:any) => {
      if (res.code === '200') {
        localStorage.setItem('user-auth', JSON.stringify(res.data.data))
        localStorage.setItem('user-info', JSON.stringify({
          ...form,
          myCheckBox: checkBox.value
        }))
        router.push({path: '/home'})
        ElMessage.success('登录成功')
      } else {
        ElMessage.error(res.message ?? '登录失败')
      }
      loading.value = false
    })
  }catch (e) {
    console.log(e)
    loading.value = false
  }
}
onMounted( () => {
  let {username, password, myCheckBox} = JSON.parse(localStorage.getItem('user-info') || '{}');
  if (username && password) {
    form.username = username
    form.password = password
    checkBox.value = Boolean(myCheckBox)
  }
})
</script>

<template>
  <div class="main">
    <div class="login">
      <div class="title">
        <div class="logo">
          <img src="@/assets/logo.svg" alt="">
        </div>
        <div class="title-text">后台管理系统</div>
      </div>
      <div class="login-container" v-loading="loading">
        <div class="login-form" >
          <div class="login-title">用户登录</div>
          <el-form :model="form"  style="width: 100%">
            <el-form-item >
              <el-input v-model="form.username" placeholder="请输入帐号">
                <template #prefix>
                  <el-icon :size="30"><User  /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item >
              <el-input v-model="form.password"  placeholder="请输入密码" type="password">
                <template #prefix>
                  <el-icon :size="30"><Lock  /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-checkbox v-model="checkBox" label="记住密码" size="large" />
            </el-form-item>
            <el-form-item>
              <el-button style="width: 100%;height: 60px"  type="primary" @click="login" >登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.main {
  width: 100vw;
  height: 100vh;
  background: url("@/assets/images/bg.png") no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  .login {
    display: flex;
    margin-top: 160px;
    width: 775px;
    height: 631px;
    flex-direction: column;
    align-items: center;
  }
  .title {
    width: 329px;
    height: 71px;
    margin-bottom: 58px;
    display: flex;
    .logo {
      width: 77px;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .title-text {
      width: 242px;
      height: 100%;
      color: #469EFC;
      font-size: 38px;
      font-weight: 600;
      line-height: 71px;
      text-align: center;
    }
  }
  .login-container {
    width: 100%;
    height: 502px;
    border: 1px solid #ccc;
    display: flex;
    justify-content: center;
    align-items: center;
    .login-form {
      width: 500px;
      height: 406px;
      display: flex;
      flex-direction: column;
      align-items: center;
      .login-title {
        color: #fff;
        font-size: 34px;
        margin-bottom: 50px;
      }
    }
  }
}
:deep(.el-input__wrapper){
  background-color:rgba(0,0,0,0);
  border: #7EB2EA;
  height: 60px;
}

:deep(.el-input__inner) {
  background-color: rgba(0, 0, 0, 0) !important;
  color: #fff;
  font-size: 20px;
}

</style>